// +build consul
package main

import (
	"fmt"
	"log"
	"time"


	"github.com/rcrowley/go-metrics"
	"github.com/smallnest/rpcx/server"
	"github.com/smallnest/rpcx/serverplugin"
	"smallnest-rpcx/advanced/services"
)

//StartServer 开启服务
func main() {
	s := server.NewServer()
	//注册插件
	addRegistryPlugin(s)

	s.RegisterName("Arith", new(services.Arith), "") //注册Arith服务
	s.RegisterFunction("s.fake.service",services.SendPong,"")	//注册为方法
	//s.RegisterFunctionName("s.fake.service2","") ???
	// s.Register(new(services.Arith), "")	//另一种注册方式, 这种方式 使用类型名作为服务名
	err := s.Serve("tcp", "0.0.0.0:8972")
	if err != nil {
		fmt.Println(err)
	}
}

func addRegistryPlugin(s *server.Server) {
	r := &serverplugin.ConsulRegisterPlugin{
		ServiceAddress: "tcp@0.0.0.0:8972",	//本地启动监听的端口
		ConsulServers: []string{	//consul的地址
			"0.0.0.0:8500",
		},
		BasePath:       "/rpcx",	//基本路径
		Metrics:        metrics.NewRegistry(),
		UpdateInterval: time.Minute,	//刷新租约  如果UpdateInterval*3没有得到更新,就会从consul中剔除
	}
	err := r.Start()
	if err != nil {
		log.Fatal(err)
	}
	s.Plugins.Add(r)
}



